package com.wgts.wgts.control.dialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/*
 *
 *   data:2018/9/269:09
 *
 */public class CancleListDialogFragment extends AbstractCustomDialogFragment {
    private static String CancleListDialogFragmentBundle = "CancleListDialogFragmentBundle";
    @BindView(R.id.dialog_title)
    TextView dialogTitle;
    @BindView(R.id.cancel_dialog_recycle)
    RecyclerView cancelDialogRecycle;
    @BindView(R.id.point_error_ly)
    LinearLayout pointErrorLy;
    Unbinder unbinder;

    private CancelListDate cancelListDate;
    private CancelListAdapter adapter;

    private onClickListen onClickListen;

    @Override
    public void onStart() {
        super.onStart();
        // 设置宽度为屏宽, 靠近屏幕底部。
        Window win = getDialog().getWindow();
        // 一定要设置Background，如果不设置，window属性设置无效
        win.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);

        WindowManager.LayoutParams params = win.getAttributes();
        params.gravity = Gravity.BOTTOM;
        // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        win.setAttributes(params);
    }

    @Override
    public View onCreateDialogView() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            cancelListDate = bundle.getParcelable(CancleListDialogFragmentBundle);
        }
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_fragment_cancel_list, null);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    public static CancleListDialogFragment getCancleListDialogFragment(CancelListDate date) {
        CancleListDialogFragment dialogFragment = new CancleListDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable(CancleListDialogFragmentBundle, date);
        dialogFragment.setArguments(bundle);
        return dialogFragment;
    }
    private void initView(){
        dialogTitle.setText(cancelListDate.getTitle());
        cancelDialogRecycle.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new CancelListAdapter(getActivity(), cancelListDate.getList());
        cancelDialogRecycle.setAdapter(adapter);
        adapter.setOnItemClickListen(new CancelListAdapter.onItemClickListen() {
            @Override
            public void onItemClick(View view, int position) {
                if (onClickListen != null){
                    onClickListen.onClick(cancelListDate.getList().get(position));

                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.point_error_ly)
    public void onViewClicked() {
        dismiss();
    }
    public interface onClickListen{
        void onClick(String s);
    }

    public void setOnClickListen(onClickListen onClickListen){
        this.onClickListen = onClickListen;
    }
}
